const app = getApp();
const apiUrl = app.appData.url;
const loadingTxt = '拼命加载中...';
const apicontentType = app.appData.contentType;
//request 请求
wxRequest = function(url, params, method) {
  method = method ? method : 'GET';
  return new Promise(function(resolve, reject) {
    wx.request({
      url: apiUrl + url,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: method,
      data: params,
      success: function(res) {
        resolve(res.data);
      }
    });
  });
}
//轮播图
getBanner = function(that) {

  wxRequest('video/banner').then((res) => {

    that.setData({
      banner: res.data,
    });
  });
} 
//视频列表
getVideoList = function (that) {
  wxRequest('video', {
    page: that.data.page,
    chanel_id: that.data.chanel_id,
    option: that.data.option,
  }).then((res) => {
    //判断是否是刷新
    if (that.data.page > 1) {
      //上拉加载更多
      that.setData({
        listData: that.data.listData.concat(res.data.data),
        totalPage: res.data.last_page,
      });
    } else {
      //下拉刷新
      wx.setStorageSync('indexDatas', res.data.data);
      wx.setStorageSync('tmpPages', res.data.last_page);
      that.setData({
        listData: res.data.data,
        totalPage: res.data.last_page,
      });
    }
    // 隐藏加载中
    wx.hideLoading();
    wx.stopPullDownRefresh();
  });
}
//收集用户formid
userFormId = function(formId, openid) {
  wxRequest('form', {
    form_id: formId,
    openid: openid
  }, 'POST');
}

//视频详情列表随机20条数据
getRandList = function(that) {
  wxRequest('video/list', {
    id: that.data.id
  }).then((res) => {
    that.setData({
      listData: res.data, //随机列表
    });
  });
}

//视频详情
getVideoInfo = function(that, txvContext, params) {
  wx.showLoading({
    title: '跳转中...'
  })
  var user = wx.getStorageSync("user");
  var openid = user.openid || 0;
  var shareuid = 0;
  if (params.shareuid) {
    shareuid = 1;
  }

  wxRequest('video/info', {
    id: that.data.id,
    openid: openid,
    shareuid: shareuid
  }).then((res) => {
    that.setData({
      infoData: res.data,
      videoShow: true,
    });
    that.txvContext = txvContext.getTxvContext('txv1');
    wx.hideLoading();
  });
}












//按钮统计
buttonCount = function(id) {
  wx.request({
    url: apiUrl + 'count',
    header: {
      'content-type': apicontentType
    },
    method: 'POST',
    data: {
      id: id
    },
  });
}




//获取 美文推荐
getMeiwen = function(that) {

  wx.request({
    url: 'https://meiwenapi.xiegangsir.com/api/article/daduizhang',
    success: function(res) {
      that.setData({
        meiwenList: res.data.data,

      })
    }
  });
}

//提示
openToast = function(msg) {
  wx.showToast({
    title: msg,
    icon: 'success',
    duration: 1000
  });
}
module.exports = {
  getVideoInfo: getVideoInfo,
  getVideoList: getVideoList,
  wxRequest: wxRequest,
  getRandList: getRandList,
  userFormId: userFormId,
  buttonCount: buttonCount,
  getMeiwen: getMeiwen,
  getBanner: getBanner,
}